import java.net.*;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.*;

public abstract class Client extends Thread {

	private String hostname;
	private int port;
	private static Socket client = null;
	private static PrintWriter out;
	private static BufferedReader in;
	private static boolean isOn;
	private Scanner s;
	

	Client() {

		s = new Scanner(System.in);

	}
	
	public boolean isOn(){
		return isOn;
	}
	

	public String getLine(){
		try{
			return in.readLine();
		}
		catch (IOException ex){
			return null;
		}

		
	}
	
/*
	public void listen() {
		
	}
*/
	
	public static void close() {
		try {
			in.close();
			out.close();
			client.close();
		} catch (IOException e) {
			System.out.println("Error while closing streams");
		}

	}



	public void sendLine(String line){
		try {
			out.println(line);
		} catch (NullPointerException e) {

		}		
	}
	

	public void connect() {

		System.out.println("Trying to connect to server " + this.hostname
				+ " on port " + port);

		try {
			client = new Socket(hostname, port);
			out = new PrintWriter(client.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(
					client.getInputStream()));

		} catch (UnknownHostException e) {
			System.out.println(hostname + "is not found");
			System.exit(0);
		} catch (IOException e3) {
			System.out.println("Cannot connect");
			System.exit(0);
		}

		System.out.println("Connected");
		isOn = true;

	}
	
	public void getData(){
		
		try {
			System.out.println("Podaj port");
			port = s.nextInt();
			System.out.println("Podaj nazwe hosta");
			hostname = s.next();
		} catch (InputMismatchException e) {
			s.close();
			return;
		}

	}

	public abstract void listen();
	
	public void run(){
		listen();
	}

	public void quit() {
		out.println("Q");
		s.close();
		isOn=false;
	}

}
